

"""
录入数据

"""



import lxml

from docx.shared import RGBColor#设置字体
from docx.oxml.ns import nsdecls
from docx.oxml import parse_xml
from docx.shared import Inches
from docx import Document
from docx.shared import Pt
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx import Document   #用来建立一个word对象
from docx.shared import Pt  #用来设置字体的大小
from docx.shared import Inches
from docx.oxml.ns import qn  #设置字体
from docx.shared import RGBColor  #设置字体的颜色
from docx.enum.text import WD_ALIGN_PARAGRAPH  #设置对其方式
# import pymysql
import xlrd
path = "/Users/v_baiguanghui/Downloads/云控相关项目/18.case改造/7.docx"
document = Document()
document.styles['Normal'].font.size = Pt(12)
document.styles['Normal'].font.name = u'Times New Roman'  # 设置西文字体
document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')  # 设置中文字体使用字体2->宋体

#用例文件地址
path_list = ["/Users/v_baiguanghui/Downloads/云控相关项目/18.case改造/云控标准版6.1集成_测试用例7.xlsx",]
#一级标题，需要手动填入
# heading_list = ['功能用例', '接口测试']
heading_list = ['路测新增事件',]

for k in range(len(path_list)):
    xlrd_path = path_list[k]
    wb = xlrd.open_workbook(xlrd_path)
    worksheet_names = wb.sheet_names()
    para_heading = document.add_heading('', level=1)  # 返回1级标题段落对象，标题也相当于一个段落
    run = para_heading.add_run(f'{k+1}.{heading_list[k]}')
    run.font.name = u'宋体'  # 设置为宋体
    run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')  # 设置为宋体，和上边的一起使用
    run.font.size = Pt(14)  # 设置1级标题文字的大小为“小四” 为12磅
    run.font.color.rgb = RGBColor(0, 0, 0)  # 设置颜色为黑色

    for i in range(110):
        try:
            sheet = wb.sheet_by_index(i)

            # name = worksheet_names[i]
            #
            # para_heading = document.add_heading('', level=2)  # 返回1级标题段落对象，标题也相当于一个段落
            # run = para_heading.add_run(f'{k+1}.{i+1} {name}')
            # run.font.name = u'宋体'  # 设置为宋体
            # run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')  # 设置为宋体，和上边的一起使用
            # run.font.size = Pt(13)  # 设置1级标题文字的大小为“小四” 为12磅
            # run.font.color.rgb = RGBColor(0, 0, 0)  # 设置颜色为黑色


        except IndexError:
            break
        try:
            for x in range(0, 1000):

                row_datas = sheet.row_values(x)
                print(row_datas)

                para_heading = document.add_heading('', level=2)  # 返回1级标题段落对象，标题也相当于一个段落

                run = para_heading.add_run(f'{6+1}.{x+1} {row_datas[0]} \n')
                run.font.name = u'宋体'  # 设置为宋体
                run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')  # 设置为宋体，和上边的一起使用
                run.font.size = Pt(12.5)  # 设置1级标题文字的大小为“小四” 为12磅
                run.font.color.rgb = RGBColor(0, 0, 0)  # 设置颜色为黑色


                table = document.add_table(rows=8, cols=3)
                table.style = 'Table Grid'
                table.rows[0].cells[0].text = f'测试用例: {row_datas[0]}'
                shading_elm_1 = parse_xml(r'<w:shd {} w:fill="e5e5e5"/>'.format(nsdecls('w')))
                table.rows[0].cells[0]._tc.get_or_add_tcPr().append(shading_elm_1)

                table.rows[1].cells[0].text = f'摘要\n{row_datas[0]}'
                table.rows[2].cells[0].text = f'前置条件：\n{row_datas[4]}'
                table.rows[3].cells[0].text = f'#:'
                table.rows[3].cells[1].text = f'步骤动作:'
                table.rows[3].cells[2].text = f'期望的结果:'
                table.rows[4].cells[0].text = f'1'
                table.rows[4].cells[1].text = f'{row_datas[5]}:'
                table.rows[4].cells[2].text = f'{row_datas[6]}'

                table.rows[5].cells[0].text = f'测试方式:'
                table.rows[5].cells[1].text = f'{row_datas[2]}'

                table.rows[6].cells[0].text = f'重要性:'
                table.rows[6].cells[1].text = f'{row_datas[1]}'

                table.rows[7].cells[0].text = f''
                # 第一行合并单元格
                a = table.cell(0, 0)
                b = table.cell(0, 1)
                c = table.cell(0, 2)
                A = a.merge(b)
                a.merge(c)
                # 第2行合并单元格

                a = table.cell(1, 0)
                b = table.cell(1, 1)
                c = table.cell(1, 2)
                A = a.merge(b)
                a.merge(c)

                a = table.cell(2, 0)
                b = table.cell(2, 1)
                c = table.cell(2, 2)
                A = a.merge(b)
                a.merge(c)

                a = table.cell(5, 1)
                b = table.cell(5, 2)
                A = a.merge(b)

                a = table.cell(6, 1)
                b = table.cell(6, 2)
                A = a.merge(b)

                a = table.cell(7, 0)
                b = table.cell(7, 1)
                c = table.cell(7, 2)
                A = a.merge(b)
                a.merge(c)
            document.add_paragraph('')

        except IndexError:
            pass

document.save(path) # 指定路径


